//============================================================================
// Name        : GSP295_Wk3_BinarySearch
// Author      : Ronald Ram
// Version     :
// Copyright   : GSP295 Wk3 Program - BinarySearch
// Description : Tree Implemented as Array, with a Binary Search in C++
//============================================================================


#include <iostream>
#include "BinarySeach.h"
using namespace std;
using namespace GSP295_Wk3;



void WelcomeMsg();
void Instructions();
void Command(char input);
void InsertToArray();
void DeleteFromArray();
void ClearArray();
void PrintArray();




bool quit = false, bInsertDeleteClearPrintQuit = false;
char yesNo, addInsertDeleteClearPrintQuit;
string input;


BinarySeach program;


int main()
{
	WelcomeMsg();

	do 
	{
		Instructions();
		cout << "Enter Command: "; cin >> addInsertDeleteClearPrintQuit;

		Command(addInsertDeleteClearPrintQuit);


	} while ( !quit );

	cout << "\n\n....Good Bye\n" << endl;
	system("pause");
	return 0;
}



//Decide on which command to trigger
void Command(char input)
{
	if ( input == 'I' || input == 'i' ) { InsertToArray();
	} 
	else if ( input == 'D' || input == 'd' ) { DeleteFromArray();
	}
	else if ( input == 'C' || input == 'c' ) { ClearArray();
	}
	else if ( input == 'P' || input == 'p' ) { PrintArray();
	}
	else if ( input == 'Q' || input == 'q' ) { quit = true;
	}
}


//Add string to Array 
void InsertToArray()
{
	bool success = false;
	cout << "\nEnter a string: "; cin >> input;
	success = program.Insert(input);

	if ( success ) 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Successfully added to Array <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;
	} 
	else 
	{
		if ( !program.isFull() ) 
		{
			cout << "\n\n======================================\n" << endl;
			cout << ">>> '" << input << "' - Insert FAILED - Violation: Tree Collision <<<" << endl;
			cout << "\n\n======================================\n\n\n\n" << endl;
		} 
		else
		{
			cout << "\n\n======================================\n" << endl;
			cout << ">>> '" << input << "' - Insert FAILED - Array is Full, or will Overflow <<<" << endl;
			cout << "\n\n======================================\n\n\n\n" << endl;
		}
	}

	program.Print_InOrder();
}

//Delete string from Array 
void DeleteFromArray()
{
	bool success = false;
	cout << "\nEnter a string: "; cin >> input;
	success = program.Delete(input);

	if ( success ) 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Successfully removed from Array <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;
	} 
	else 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Delete FAILED - No Match Found <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;
	}

	program.Print_InOrder();
}

//Print sorted data
void PrintArray() 
{
	program.Print_InOrder();
}

//Clear all elements in Array
void ClearArray()
{
	cout << "\n\n======================================" << endl;
	cout << " >>>> RESULTS: <<<<" << endl; 
	cout << "======================================\n" << endl;

	program.Print_InOrder();

	cout << "\n\n======================================\n\n\n\n" << endl;
}


//Welcome Message
void WelcomeMsg()
{
	cout << "\n**************************************************" << endl;
	cout << "\t GSP295 Week 3: iLab Assignments" << endl;
	cout << "\n\t Title: Binary Search - Program 3" << endl;
	cout << "\n\t Author: Ronald Ram" << endl;
	cout << "**************************************************\n\n\n" << endl;
}


//Instructions for Command keys to use
void Instructions()
{
	cout << "---------------------------------------" << endl;
	cout << "Command Options: " << endl;
	cout << "---------------------------------------" << endl;
	cout << "Insert[I] - Delete[D] - Print[P] - Clear[C]" << endl;
	cout << "Q -> Quit" << endl;
	cout << "---------------------------------------\n" << endl;
}

